package com.jhh.pay.center.actor;

import com.jhh.pay.center.api.data.StateRequest;
import com.jhh.pay.center.model.Order;
import com.jhh.pay.center.model.OrderState;
import com.jhh.pay.center.model.PayChannel;
import com.jhh.pay.center.model.TradeState;
import com.jhh.pay.center.service.OrderService;
import com.jhh.pay.center.service.PayChannelCfgService;
import com.jhh.pay.center.service.PayChannelService;
import com.jhh.pay.center.service.cloud.PayChannelTemplate;
import com.jhh.pay.center.service.cloud.message.TradeResult;
import com.jhh.pay.center.service.cloud.message.TradeStateBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author tangxd
 * @Description: 状态查询ACTOR
 * @date 2017/12/13
 */
@Service("StateActor")
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Slf4j
public class StateActor extends TradeActor {

    @Autowired
    private PayChannelTemplate template;

    @Autowired
    private PayChannelCfgService cfgService;

    @Autowired
    private PayChannelService channelService;

    @Autowired
    private OrderService orderService;

    @Override
    public Receive createReceive() {
        return receiveBuilder()
                .match(StateRequest.class, r -> {

                    Order order = orderService.get(r.getSid());
                    if (null == order) {
                        log.warn("流水号" + r.getSid() + "订单不存在");
                        sender().tell(TradeResult.create(TradeState.ERROR, "SYS:STE:0001", "流水号订单不存在"), self());
                        context().stop(self());
                        return;
                    }

                    if(order.getState() == OrderState.SUCCESS) {
                        sender().tell(TradeResult.create(TradeState.SUCCESS, "SUCCESS", "订单已成功"), self());
                        context().stop(self());
                        return;
                    }

                    PayChannel channel = channelService.get(order.getChannelId());
                    if (null == channel) {
                        log.warn("流水号" + r.getSid() + "支付通道未确认");
                        sender().tell(TradeResult.create(TradeState.ERROR, "SYS:STE:0002", "订单支付通道未确认"), self());
                        context().stop(self());
                        return;
                    }

                    TradeStateBody tp = new TradeStateBody();
                    tp.setSid(r.getSid());
                    if (!setChannelCfg(tp, channel)) {
                        return;
                    }
                    setMessageExtension(tp, channel, order);

                    try {
                        TradeResult mr = template.state(channel, tp, order.getType());

                        /**
                         * 保存订单状态
                         * 订单未完成状态下更新成成功状态
                         */
                        if (mr.getState() == TradeState.SUCCESS && order.getState() != OrderState.SUCCESS) {
                            order.setState(OrderState.SUCCESS);
                            order.setSuccessTime(new Date());
                            orderService.update(order);
                        }
                        sender().tell(mr, self());
                    } catch (Exception e) {
                        e.printStackTrace();
                        log.error(e.getMessage(), e);
                        sender().tell(TradeResult.create(TradeState.ERROR, "SYS:STE:0002", "订单支付通道未确认"), self());
                    }
                    context().stop(self());
                })
                .build();
    }
}
